/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import hibernate.entity.PriceHistory;
import hibernate.entity.TrackedProduct;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author NamDH
 */
public class PriceHistoryDAO {

    private Session session;

    public PriceHistoryDAO() {
        SessionFactory sf = Common.HibernateUtil.getSessionFactory();
        session = sf.getCurrentSession();
    }

    public double getLastPriceHistorybyTPID(int trackedProductID) {
        try {

            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();

            session.getTransaction().begin();
            String sql = "from PriceHistory where TrackedProductID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, trackedProductID);
            List<PriceHistory> prices = query.list();
            double result = 0;

            for(PriceHistory item:prices){
                result=item.getPrice();
            }
           // return prices.get(prices.size() - 1).getPrice();
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public boolean addNewPriceHistory(TrackedProduct trackedProduct, double price) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();

            session.getTransaction().begin();
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String updateDate = dateFormat.format(date);
            //String sql = "insert into(TrackedProductID, Price, UpdatedDate) values (?, ?, ?)";
            PriceHistory pricehistory = new PriceHistory(trackedProduct, price, updateDate);
            session.save(pricehistory);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean deletePricebyProductID(int trackedProductID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "delete PriceHistory where TrackedProductID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, trackedProductID);
            query.executeUpdate();
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }
}
